package com.suixinsanren.design.template.demo03;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public abstract class CaffeineBeverageWithHook {

    protected void prepareRecipe() {
        boilWater();
        brew();
        pourInCup();
        if (customerWantsCondiments()) {
            addCondiments();
        }
    }

    protected boolean customerWantsCondiments() {
        return getUserInput().startsWith("y");
    }

    protected String getUserInput() {
        String answer = null;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("是否需要添加加料?");
        try {
            answer = bufferedReader.readLine();
        } catch (IOException e) {
            System.out.println("接收用户输入失败");
        }
        return answer == null ? "no" : answer;
    }

    protected abstract void addCondiments();

    protected void pourInCup() {
        System.out.println("倒入杯子里");
    }

    protected abstract void brew();

    protected void boilWater() {
        System.out.println("把水煮沸");
    }
}
